{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from data import get_data\n",
    "import numpy as np\n",
    "from keras.preprocessing import sequence\n",
    "from keras.models import load_model\n",
    "\n",
    "\n",
    "date_set = get_data()\n",
    "features = [i[1] for i in date_set]  # 提取域名\n",
    "label = [i[0] for i in date_set]  # 提取标签\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ready_data():\n",
    "\n",
    "    valid_chars = {x: idx + 1 for idx, x in enumerate(set(''.join(features)))}  # 构造检索字典\n",
    "    maxlen = np.max([len(x) for x in features])  # 获得输入特征的最大长度\n",
    "    print(valid_chars, max_features, )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_Y(label):\n",
    "    return [0 if x == 'benign' else 1 for x in label]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_X(features):\n",
    "    valid_chars, maxlen = ready_data()\n",
    "    X = [[valid_chars[y] for y in x] for x in features]\n",
    "    X = sequence.pad_sequences(X, maxlen=maxlen)\n",
    "    X = np.array(X)\n",
    "    return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def DGA_prodect():\n",
    "    x = get_features()[0: 10000]\n",
    "    X = process_X(x)\n",
    "    model = load_model('./model/DGA_predict_LSTM_V1.h5')\n",
    "    prodect_label = model.predict(X)\n",
    "    print(prodect_label)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
